package com.lm.scau.framedemos;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.lm.scau.framedemos.Adapter.MainAdapter;
import com.lm.scau.framedemos.Views.LMTitleBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends BaseActivity {
    private ListView mListView;
    private MainAdapter mAdapter;
    private List<String> titles;//首列表名
    private List<String> itemInfos;// 具体项目
    private long firstTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.main_list_view);
        titles = new ArrayList<>();
        itemInfos = new ArrayList<>();
        mTitleBar = (LMTitleBar) findViewById(R.id.titlebar);
        initData();
        initAction();
    }

    private void initAction() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String itemInfo = itemInfos.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("itemInfo", itemInfo);
                bundle.putString("title", titles.get(position));
                startActivity(GridActivity.class, bundle);
            }
        });
    }

    private void initData() {
        Collections.addAll(titles, getResources().getStringArray(R.array.head_titles));
        Collections.addAll(itemInfos, getResources().getStringArray(R.array.itme_infos));
        if (titles.size() != itemInfos.size()) {
            showToast("主demo 列表数据未填写完整！");
            return;
        }
        mAdapter = new MainAdapter(titles, itemInfos, this);
        mListView.setAdapter(mAdapter);
        mTitleBar.setTitle("首页");
        mTitleBar.hideBackView();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {                                         //如果两次按键时间间隔大于2秒，则不退出
                    showToast("再按一次退出程序");
                    firstTime = secondTime;//更新firstTime
                    return true;
                } else {                                                    //两次按键小于2秒时，退出应用
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
